package com.t4l.server.persistence;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;

import com.t4l.server.ApplicationListener;
import com.t4l.shared.exceptions.T4LException;

public class PersistenceUtil {

	public static <T extends T4LEntity> T saveEntity(T entity) throws T4LException {
		try {
			EntityManager em = ApplicationListener.getEM();
			EntityTransaction tx = em.getTransaction();
			tx.begin();
			em.persist(entity);
			em.flush();
			tx.commit();
			return entity;
		} catch (Throwable e) {
			e.printStackTrace();
			throw new T4LException("Error al persitir la entidad.", e);
		}
	}

}
